﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dbPoll_Application.Models.AlphanumericQuestion>" validateRequest="false" %>
<%@ Import Namespace="dbPoll_Application.Helpers" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Edit Alphanumeric Question
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div class="pageHeadingContainer"><h2 class="pageHeading">Question Editor</h2></div>
    <div id="contentContainer">
        <p>
           <%= Html.ActionLink("Polls > ", "Index", "Poll", null, new { @class = "breadcrumb" })%>
           <%= Html.ActionLink(ViewData["pollName"] + " > ", "Questions", "Poll", new { pollID = ViewData["pollID"], pollName = ViewData["pollName"] }, new { @class = "breadcrumb" })%>
           Question <%= Model.questionNumber %>
        </p>
    <script src="../../Scripts/EditSAQ.js" type="text/javascript"></script>
    
    <% Html.EnableClientValidation(); 
       var pollRoles = ViewData["pollRoles"] as ICollection<dbPoll_Application.Models.Role>;
       var pollCreator = new dbPoll_Application.Models.Role("Poll Creator");
       var pollAdministrator = new dbPoll_Application.Models.Role("Poll Administrator");
       var pollStart = ViewData["pollStart"] as DateTime?;
       var pollClose = ViewData["pollClose"] as DateTime?;%>
    <% if (Model.questionID < 0)
       {
           Html.RenderPartial("PageMenu", Model);
       } %>
    <% String actionName;
       if (Model.questionID < 0)
       {
           actionName = "AddAlphanumeric";
       }
       else {
           actionName = "UpdateAlphanumeric";
       } %>
    <%  using (Ajax.BeginForm(actionName, new AjaxOptions { OnSuccess = "onSubmitSuccess" }))
        { %>
        <input type="hidden" id="pollID" value="<%= ViewData["pollID"] %>" name="pollID" />
        <input type="hidden" id="pollName" value="<%= ViewData["pollName"] %>" name="pollName" />
        <div id="questionEditorContainer">
            <%= Html.HiddenFor(m => m.questionID)%>
            <%= Html.HiddenFor(m => m.creationTime)%>
            <%= Html.HiddenFor(m => m.deletionTime)%>
            <%= Html.HiddenFor(m => m.type)%>
            <%= Html.HiddenFor(m => m.questionNumber)%>
        
            <ul id="editMCQTable">
                <li id="questionRow"><%= Html.TextAreaFor(m => m.question)%></li>
            </ul>
            <div id="optionContainer">
                <ul id="optionList">
                    <% for (int i = 0; i < Model.answers.Count; i++) 
                    { %>
                    <li class="option">
                        <span class="optionNumber"><%= i + 1%>.</span>
                        <% if ((pollRoles.Contains(pollCreator) || pollRoles.Contains(pollAdministrator)) && pollStart==null)
                           { %><span class="optionControls"><span class="deleteOption"><img src="../../Resources/remove.png" /></span><%} %>
                        </span>
                        <span><%= Html.TextAreaFor(m => m.answers[i].answer, new { @class = "optionTextarea" })%></span><span><%= Html.HiddenFor(m => m.answers[i].answerID) %></span>
                   </li>
                <% } %>
                </ul>
                <% if ((pollRoles.Contains(pollCreator) || pollRoles.Contains(pollAdministrator)) && pollStart==null)
                   { %><textarea cols="1" rows="20" id="addOptionText"></textarea>
                <% if (Model.answers.Count < 5)
                { %>
                <input type="button" value="Add Answer" id="addOptionButton" />
                <% }
                else
                { %>
                <input type="button" value="Add Answer" id="addOptionButton" disabled="true" />
                <% } %><%} %>
            </div>
            <div id="buttons">
                <ul>
                   <%  var saved = ViewData["saved"] as String;
                   if (saved == "true") { %>
                   <li class="left"><input type="submit" value="Saved" id="submitForm" disabled="disabled" /><span id="result"></span><span id="errorMessage"></span><span><%= Html.ValidationMessageFor(m => m.question)%></span></li>
                   <script type="text/javascript">
                       $(document).click(function() {
                           $('#submitForm').val("Save");
                           $('#submitForm').attr("disabled", false);
                           $(document).unbind('click');
                       });
                   </script>
                   <% } 
                      else { %>
               <li class="left"><input type="submit" value="Save" id="submitForm" /><span id="result"></span><span id="errorMessage"></span><span><%= Html.ValidationMessageFor(m => m.question)%></span></li>
               <% } %>
                </ul>
            </div>
        </div>
        <div id="rightColumn">
        <% Html.RenderPartial("QuestionObjects", Model); %>
        <% Html.RenderPartial("QuestionSettings", Model); %>
        <% Html.RenderPartial("ChartSettings", Model); %>
        </div>
        <div class="clear"></div>
        <% } %>
        </div> 
        <script type="text/javascript" >
        <%            
            if  ((!(pollRoles.Contains(pollCreator) || pollRoles.Contains(pollAdministrator)) || pollStart!=null) && ViewData["testPoll"] == "False") 
            {%>document.getElementById("question").readOnly=true;
          <%}
            if (pollClose>DateTime.Now)
            {  
                int it=0;
                foreach (var answers in Model.answers) {%>
                    document.getElementById("answers_<%=it%>__answer").readOnly=true; <%it++;
                }
            }%>
        </script>
</asp:Content>